from common.ClickHouseClient import CkClient
from clickhouse_sqlalchemy import make_session
from datetime import datetime, tzinfo, timedelta
import time
import pytz
"""
tzinfo是关于时区信息的类
tzinfo是一个抽象类，所以不能直接被实例化
"""
class UTC(tzinfo):
    """UTC"""
    def __init__(self,offset = 0):
        self._offset = offset

    def utcoffset(self, dt):
        return timedelta(hours=self._offset)

    def tzname(self, dt):
        return "UTC +%s" % self._offset

    def dst(self, dt):
        return timedelta(hours=self._offset)
class CkOperator:
    def __int__(self):
        self.ck = CkClient()

    def batch_insert(self, task_list):
        ck = CkClient()
        list = []
        sql = "insert into daily.t_diary (task_name, task_type, task_subtype, task_content, task_important_flg, " \
              "task_plan_flg, task_priority, task_start, task_end, task_gain, task_elapse, create_time) values"
        for task in task_list:
            task_name = task[1]
            if task_name is None:
                task_name = ""
            task_type = task[2]
            if task_type is None:
                task_type = ""
            task_subtype = task[3]
            if task_subtype is None:
                task_subtype = ""
            task_content = task[4]
            if task_content is None:
                task_content = ""
            task_important_flg = task[5]
            if task_important_flg is None:
                task_important_flg = ""
            task_plan_flg = task[6]
            if task_plan_flg is None:
                task_plan_flg = ""
            task_priority = task[7]
            if task_priority is None:
                task_priority = ""
            task_start = task[8]
            if task_start is None:
                task_start = datetime(1900, 1, 1, 0, 0, 0, tzinfo=UTC(8))
            task_start = datetime.strptime(task_start, "%Y-%m-%d %H:%M:%S")
            task_end = task[9]
            if task_end is None:
                task_end = datetime(1900, 1, 1, 0, 0, 0, tzinfo=UTC(8))
            task_end = datetime.strptime(task_end, "%Y-%m-%d %H:%M:%S")
            task_gain = task[10]
            if task_gain is None:
                task_gain = ""
            task_time = task[11]
            print(task_time)
            if task_time is None:
                task_time = 0
            task_time = int(task_time)
            timezone = 'Asia/Shanghai'
            tz = pytz.timezone(timezone)
            create_time = datetime.now(tz)
            data = dict(task_name=task_name, task_type=task_type, task_subtype=task_subtype, task_content=task_content,
                        task_important_flg=task_important_flg, task_plan_flg=task_plan_flg, task_priority=task_priority,
                        task_start=task_start, task_end=task_end, task_gain=task_gain, task_elapse=task_time,
                        create_time=create_time)
            # print(data)
            list.append(data)
        # sql = """
        # insert into daily.t_diary (task_name, task_type, task_subtype, task_content, task_important_flg,  \
        #       task_plan_flg, task_priority, task_start, task_end, task_gain, task_elapse) values;
        # """
        print(list[0])
        ck.connect.executemany(sql, list)
        # data = dict(task_name="jjjjj")
        # ck.connect.execute(sql, data)
        print("Done!")

    # def insert_item(self, task, ck):
    #     task_name = task[1]
    #     task_type = task[2]
    #     task_subtype = task[3]
    #     task_content = task[4]
    #     task_import_flg = task[5]
    #     task_plan_flg = task[6]
    #     task_priority = task[7]
    #     task_start = task[8]
    #     task_end = task[9]
    #     task_gain = task[10]
    #     task_time = task[11]

        # sql = "insert into task_info (task_name, task_type, task_subtype, task_content, task_important_flg, " \
        #       "task_plan_flg, task_priority, task_start, task_end, task_gain, task_elapse) values ( %s, %s, %s, %s, " \
        #       "%s, %s, %s, %s, %s, %s, %s)".format(task_name, task_type, task_subtype, task_content, task_import_flg,
        #         task_plan_flg, task_priority, task_start, task_end, task_gain, task_time)




